import { useUserStore } from "@/stores/user";
import { createRouter, createWebHistory } from "vue-router";
import nProgress from "nprogress";
import "nprogress/nprogress.css";
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/register",
      name: "register",
      component: () => import("../views/register/index.vue"),
      meta: { title: "注册页" },
    },
    { path: "/login", component: () => import("@/views/login/index.vue") },
    {
      path: '/user/patient',
      component: () => import('@/views/user/patient.vue'),
      meta: { title: '家庭档案' }
    },
    {
      path: '/consult/fast',
      component: () => import('@/views/consult/index.vue'),
      meta: { title: '极速问诊' }
    },
    {
      path: '/consult/dep',
      component: () => import('@/views/consult/consultDep.vue'),
      meta: { title: '选择科室' }
    },//如果你不知道一个人爱不爱你，那么她一定不爱你，你在火炉旁取暖怎么会
    {
      path: '/consult/illness',
      component: () => import('@/views/consult/diseaseDescription.vue'),
      meta: { title: '病情描述' }
    },
    
    {
      path: "/",
      component: () => import("@/views/main/index.vue"),
      redirect: "/home",
      children: [
        {
          path: "/home",
          component: () => import("@/views/home/index.vue"),
          meta: { title: "首页" },
        },
        {
          path: "/article",
          component: () => import("@/views/article/index.vue"),
          meta: { title: "健康百科" },
        },
        {
          path: "/notify",
          component: () => import("@/views/notify/index.vue"),
          meta: { title: "消息通知" },
        },
        {
          path: "/user",
          component: () => import("@/views/user/index.vue"),
          meta: { title: "个人中心" },
        },
      ],
    },
  ],
});

// 导航守位
router.beforeEach((to) => {
  nProgress.start();
  const store = useUserStore();
  const wihteList = ["/login", "register"];
  if (!store.user?.token && !wihteList.includes(to.path)) return "/login";
});

// 更改标题
router.afterEach((to) => {
  document.title = `${to.meta.title || ""}-优医问诊`;
  nProgress.done();
});

export default router;
